package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Employee;
import com.example.mapper.EmployeeMapper;
import com.example.service.IEmployeeService;
import com.example.service.ex.EmployeeException.*;
import com.example.service.ex.InsertException;
import com.example.service.ex.UpdateException;
import com.example.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 员工相关业务
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public Employee login(Employee employee) {
        LambdaQueryWrapper <Employee> lambdaQueryWrapper = new LambdaQueryWrapper<Employee>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        //根据用户名查询数据
        Employee employee1 = employeeMapper.selectOne(lambdaQueryWrapper);
        //判断用户名是否存在
        if (employee1==null){
            throw new UserNameNotFondException("你输入的用户名不存在");
        }
        //判断密码是否正确
        String password = employee.getPassword();
        //为输入的密码进行加密并判断
        password= MD5Util.getMD5(password);
        //判断密码是否正确
        if (!employee1.getPassword().equals(password)){
            throw new PasswordNotFondException("您输入的密码不正确，请重新输入");
        }
        //判断用户是否被禁用
        if (employee1.getStatus()==0){
            throw new UserNotFondException("您的用户已被禁用");
        }
        return employee1;
    }

    /**
     * 添加员工
     * @param employee 用户信息
     */
    @Override
    public void save(Employee employee,Long id) {
        //根据传递的用户名查询数据
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<Employee>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        //根据条件查询
        Employee employee1 = employeeMapper.selectOne(lambdaQueryWrapper);
        //判断对象是否存在
        if (employee1!=null){
            throw new UsernameException("用户名以存在");
        }
        //定义默认密码
        String password = "123456";
        //对密码进行MD5加密
        password = MD5Util.getMD5(password);
        //补全数据
        employee.setPassword(password);
        //执行添加方法进行添加数据
        int insert = employeeMapper.insert(employee);
        if (insert!=1){
            throw new InsertException("添加数据时出现未知的异常");
        }
    }

    @Override
    public Page<Employee> page(Integer page, Integer pageSize, String name) {
        //创建分页对象并给出当前页，每页显示条数
        Page<Employee> pageInfo = new Page<Employee>(page,pageSize);
        //创建条件查询对象，并把条件给对象
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<Employee>();
        lambdaQueryWrapper.eq(name!=null,Employee::getName,name);
        //添加一个降序条件
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        //调用MP方法
        employeeMapper.selectPage(pageInfo,lambdaQueryWrapper);
        return pageInfo;
    }

    @Override
    public void update(Employee employee, Long id) {
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getId,employee.getId());
        //判断需要启用/禁用的id是否存在
        Employee employee1 = employeeMapper.selectOne(lambdaQueryWrapper);
        if (employee1==null){
            throw new UserNotFondException("用户不存在");
        }
        //调用mp的方法进行修改数据
        int update = employeeMapper.update(employee, lambdaQueryWrapper);
        //判断修改是否成功
        if (update!=1){
            throw new UpdateException("在数据更新时出现未知的异常");
        }
    }

    @Override
    public Employee select(Long id) {
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getId,id);
        //判断需要启用/禁用的id是否存在
        Employee employee = employeeMapper.selectOne(lambdaQueryWrapper);
        if (employee==null){
            throw new UserNotFondException("用户不存在");
        }
        return employee;
    }
}
